// pages/shijihai/unit6/case6-2-3/case6-1-3.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileID:'',
    cloudPath:'',
    imagePath:'',
    downloadFilePath:'',
    uploadSuccess:false,
    downloadSuccess:false
  },
  doUpload:function () {
    var that=this;
    const fileID=this.data.fileID;
    if(fileID !=''){
      wx.cloud.deleteFile({
        fileList:[fileID]
      })
    }
    wx.chooseImage({
      count: 1,
      sizeType:['compressed'],
      sizeType:['album','camera'],
      success:function (res) {
        wx.showLoading({
          title: '上传中',
        })
        const filePath=res.tempFilePaths[0]
        console.log('filePath:',filePath)
        const cloudPath='img'+Date.now()+filePath.match(/\.[^.]+?$/)[0]
        wx.cloud.uploadFile({
          cloudPath,
          filePath,
          success:res=>{
            console.log('[上传文件]成功:',res)
            that:setData({
              uploadSuccess:true,
              downloadSuccess:false,
              fileID:res.fileID,
              cloudPath:cloudPath,
              imagePath:filePath,
              downloadFilePath:''
            })
          },
          fail:e=>{
            console.log('[上传文件]失败:',res)
            that:setData({
              uploadSuccess:false,
              fileID:'',
              cloudPath:'',
              imagePath:''
           })
           wx.showToast({
             icon:'none',
             title: '上传失败',
           })
          },
          complete:()=>{
            wx.hideLoading()
          }
        })
      },
      doDownload:function () {
        var that=this;
        wx.showLoading({
          title: '下载中',
        })
        wx.cloud.downloadFile({
          fileID:that.data.fileID,
          success:res=>{
            console.log("下载文件成功:",res)
            that.setDtata({
              downloadSuccess:true,
              downloadFilePath:res.tempFilePath
            })
            wx:wx.showModal({
              title: '文件下载成功',
              content:"文件路径："+that.data.downloadFilePath,
              showCancel:false,
              confirmText:'确定',
              confirmColor:"#0000ff",
            })
          },
          fail:err=>{
            that.setData({
              downloadSuccess:false,
              downloadFilePath:''
            })
          },
          complete:()=>{
            wx.hideLoading()
          }
        })
      },
      previewImage:function () {
        wx.previewImage({
          current: "",
          urls:[this.data,downloadFilePath]
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})